package com.zzzyh.forumsystemclient.cotroller;

import com.alibaba.fastjson.JSON;
import com.zzzyh.forumsystemclient.common.AppResult;
import com.zzzyh.forumsystemclient.common.ResultCode;
import com.zzzyh.forumsystemclient.model.LoginUser;
import com.zzzyh.forumsystemclient.model.User;
import com.zzzyh.forumsystemclient.model.User_Article;
import com.zzzyh.forumsystemclient.services.IArticleService;
import com.zzzyh.forumsystemclient.services.IUserArticleService;
import com.zzzyh.forumsystemclient.services.IUserService;
import com.zzzyh.forumsystemclient.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 郑雨杭
 * @create 2025/6/3 - 16:21
 */
@Slf4j
@RestController
@RequestMapping("/userArticle")
public class UserArticleController {

    @Autowired
    private IUserArticleService userArticleService;

    @Autowired
    private IUserService userService;
    @Autowired
    private IArticleService articleService;
    @RequestMapping("/getUserArticleRelationship")
    public AppResult<Integer> getUserArticleRelationship(@RequestHeader("Authorization") String token,
                                                         @RequestParam("articleId") Long articleId){
        String userString = JwtUtils.getSubjectFromToken(token);
        LoginUser user = JSON.parseObject(userString, LoginUser.class);
        if (user == null || user.getUser() == null || user.getUser().getId() == null
         || articleId == null)
            return AppResult.failed(ResultCode.FAILED_PARAM_ERROR);
        log.info("user:{},articleId:{}",user,articleId);
        return AppResult.success(userArticleService.getUserArticleRelationship(user.getUser().getId(),articleId));
    }

    @RequestMapping("/updateUserArticleRelationship")
    public AppResult<Void> updateUserArticleRelationship(@RequestHeader("Authorization") String token,
                                                         @RequestParam("articleId") Long articleId,
                                                         @RequestParam("relationship") Integer relationship){
        String userString = JwtUtils.getSubjectFromToken(token);
        LoginUser user = JSON.parseObject(userString, LoginUser.class);
        if (user == null || user.getUser() == null || user.getUser().getId() == null
                || articleId == null || relationship == null)
            return AppResult.failed(ResultCode.FAILED_PARAM_ERROR);
        Long userId   = user.getUser().getId();
        log.info("userId:{},articleId:{},relationship:{}",userId,articleId,relationship);
        User_Article userArticle = userArticleService.getUserArticle(userId, articleId);
        Integer oldRelationship = userArticle == null ? 5 : userArticle.getRelationship();
        if (userArticle == null){
            //插入关系
            User_Article userArticle1 = new User_Article();
            userArticle1.setUserId(userId);
            userArticle1.setArticleId(articleId);
            userArticle1.setRelationship(relationship);
            userArticleService.insetUserArticle(userArticle1);
        }else{
            userArticle.setRelationship(relationship);
            userArticleService.updateUserArticleRelationship(userArticle);
        }
        User articleUser = userService.selectUserByUserId(articleService.gerUserIdByArticleId(articleId));

        log.info("{}",articleUser);
        //0是我的文章1喜欢2不喜欢3收藏4收藏+喜欢5无关
        if(oldRelationship == 1) {
            if (relationship == 1){

            }else if (relationship == 2){
                articleUser.setLikeCount(articleUser.getLikeCount()-1);
                articleUser.setDislikeCount(articleUser.getDislikeCount()+1);
            }else if (relationship == 3){
                articleUser.setLikeCount(articleUser.getLikeCount()-1);
                articleUser.setCollectCount(articleUser.getCollectCount()+1);
            }else if (relationship == 4){
                articleUser.setCollectCount(articleUser.getCollectCount()+1);
            }else if (relationship == 5){
                articleUser.setLikeCount(articleUser.getLikeCount()-1);
            }
        }else if(oldRelationship == 2){
            if (relationship == 1){
                articleUser.setLikeCount(articleUser.getLikeCount()+1);
                articleUser.setDislikeCount(articleUser.getDislikeCount()-1);
            }else if (relationship == 2){

            }else if (relationship == 3){
                articleUser.setDislikeCount(articleUser.getDislikeCount()-1);
                articleUser.setCollectCount(articleUser.getCollectCount()+1);
            }else if (relationship == 4){
                articleUser.setCollectCount(articleUser.getCollectCount()+1);
                articleUser.setDislikeCount(articleUser.getDislikeCount()-1);
                articleUser.setLikeCount(articleUser.getLikeCount()+1);
            }else if(relationship == 5){
                articleUser.setDislikeCount(articleUser.getDislikeCount()-1);
            }
        }else if(oldRelationship == 3){
            if (relationship == 1){
                articleUser.setLikeCount(articleUser.getLikeCount()+1);
                articleUser.setCollectCount(articleUser.getCollectCount()-1);
            }else if (relationship == 2){
                articleUser.setDislikeCount(articleUser.getDislikeCount()+1);
                articleUser.setCollectCount(articleUser.getCollectCount()-1);
            }else if (relationship == 3){

            }else if (relationship == 4){
                articleUser.setLikeCount(articleUser.getLikeCount()+1);
            }else if (relationship == 5){
                articleUser.setCollectCount(articleUser.getCollectCount()-1);
            }
        } else if (oldRelationship == 4) {
            if (relationship == 1){
                articleUser.setCollectCount(articleUser.getCollectCount()-1);
            }else if (relationship == 2){
                articleUser.setCollectCount(articleUser.getCollectCount()-1);
                articleUser.setDislikeCount(articleUser.getDislikeCount()+1);
                articleUser.setLikeCount(articleUser.getLikeCount()-1);
            }else if (relationship == 3){
                articleUser.setLikeCount(articleUser.getLikeCount()-1);
            }else if (relationship ==5){
                articleUser.setCollectCount(articleUser.getCollectCount()-1);
                articleUser.setLikeCount(articleUser.getLikeCount()-1);
            }
        }else if (oldRelationship == 5){
            if (relationship == 1){
                articleUser.setLikeCount(articleUser.getLikeCount()+1);
            }else if (relationship == 2){
                articleUser.setDislikeCount(articleUser.getDislikeCount()+1);
            }else if (relationship == 3){
                articleUser.setCollectCount(articleUser.getCollectCount()+1);
            }else if (relationship == 4){
                articleUser.setCollectCount(articleUser.getCollectCount()+1);
                articleUser.setLikeCount(articleUser.getLikeCount()+1);
            }else if (relationship == 5){

            }
        }
        userService.updateUser(articleUser);


        return AppResult.success();
    }


}
